package com.huluobo.a2207douyindemo;

import android.Manifest;
import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.huluobo.a2207douyindemo.adapter.CommonPagerAdapter;
import com.huluobo.a2207douyindemo.databinding.ActivityMainBinding;
import com.huluobo.a2207douyindemo.home.HomeFragment;
import com.huluobo.a2207douyindemo.message.MessageFragment;
import com.huluobo.a2207douyindemo.mine.MineFragment;
import com.huluobo.a2207douyindemo.startLive.StartLiveActivity;
import com.huluobo.a2207douyindemo.videos.VideosFragment;
import com.huluobo.base.mvvm.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding> {
    private CommonPagerAdapter commonPagerAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void initData() {
        fragmentList.clear();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new VideosFragment());
        fragmentList.add(new MessageFragment());
        fragmentList.add(new MineFragment());

        commonPagerAdapter = new CommonPagerAdapter(this, fragmentList);
        binding.mainVp2.setAdapter(commonPagerAdapter);
    }

    @Override
    protected void initView() {
        requestPermissions(new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO
        }, 102);
        binding.mainVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        binding.mainBottomNav.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        binding.mainBottomNav.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        binding.mainBottomNav.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        binding.mainBottomNav.getMenu().getItem(4).setChecked(true);
                        break;
                }
            }
        });

        binding.mainBottomNav.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.main_tab_home:
                    binding.mainVp2.setCurrentItem(0);
                    break;
                case R.id.main_tab_videos:
                    binding.mainVp2.setCurrentItem(1);
                    break;
                case R.id.main_tab_start_live:
                    startActivity(new Intent(this, StartLiveActivity.class));
                    break;
                case R.id.main_tab_message:
                    binding.mainVp2.setCurrentItem(2);
                    break;
                case R.id.main_tab_mine:
                    binding.mainVp2.setCurrentItem(3);
                    break;
            }
            return true;
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.mainVp2.setCurrentItem(1);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}